package com.hhsjdesign.dynamicui.core.widgetimpl.specwidget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.hhsjdesign.dynamicui.annoprocess.AWidgetType
import com.hhsjdesign.dynamicui.interfaces.RequestListener
import com.hhsjdesign.dynamicui.businessdata.BaseModel
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.ShapeProperty
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.utils.BusinessViewCreator
import com.hhsjdesign.dynamicui.utils.BusinessViewCreatorFactory


/**
 * 强关联业务组件，这里不设置组件属性
 */
@AWidgetType("topic")
class TopicWidget(
    context: Context,
    data: WidgetData
) : FixedBusinessWidget(context, data) {

    private lateinit var listView: LinearLayout
    private val viewHolder: BusinessViewCreator<*>?

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        return LayoutInflater.from(context).inflate(0, null).apply {
            listView = findViewById(0)
        }
    }

    init {
        viewHolder = BusinessViewCreatorFactory.makeViewHolder("topic")
    }

    override fun layout(property: ShapeProperty, parentType: String) {
    }

    override fun updateLayoutParam(viewGroup: ViewGroup.LayoutParams) {
    }

    override fun onLoadData() {

    }

    override fun loadData() {
        super.loadData()
        loadRemoteBusiness()

    }

    override fun loadRemoteBusiness() {
        dataCaller?.loadData(
            "", "", this, object : RequestListener<Any> {

                override fun onRequestSuccess(requestId: String, result: BaseModel<Any>) {
                    //数组
                    if (result.code == BaseModel.SUCCESS && result.type == BaseModel.ARRAY) {
                        val dataList = result.resultList as ArrayList<IViewable<*>>
                        dataList!!.forEach {
                            val view: View = viewHolder!!.createView(
                                context,
                                "topic",
                                "topic",
                                widgetData.itemPropertyList!![0].itemPropertyModel,
                                listView
                            )
                            listView.addView(view);
                            viewHolder!!.bindDataAndEvent(
                                view,
                                it,
                                widgetData.itemPropertyList!![0].itemWidgetType,
                                widgetData.business,
                                widgetData.itemPropertyList!![0].itemPropertyModel
                            )
                        }

                    }
                }

                override fun onRequestFail(
                    requestId: String,
                    resultCode: Int,
                    resultMessage: String
                ) {
                }

            }
        );
    }

    override fun loadLocalBusiness() {
        super.loadLocalBusiness()
    }

    override fun displayBg(imageView: ImageView, url: String) {

    }

    override fun cancelPlayBg() {

    }
}